const path = require('path');
const log4js = require('log4js');

class Logger {
    //客户端ip |请求地址 |请求方法 |HTTP版本 |请求状态 |响应时间 |请求来源 |请求内容大小 |浏览器ua
    private logFormat = [':remote-addr', ':url', ':method', ':http-version', ':status', ':response-time', ':referrer', ':content-length', ':user-agent'];
    private logPath = path.join(__dirname, "../log/");
    constructor() {
        //配置日志信息
        log4js.configure({
            appenders: [{ type: 'console' }, this.appender('app'), this.appender('access', true), this.appender('error', true)],//配置app日志、访问日志、错误日志
            // levels: {
            //     "error": "ERROR"
            // },
            replaceConsole: true
        });
    }

    //日志类型
    private appender(logName: string, isWorker?: boolean) {
        let logId = '';
        if (isWorker) {
            logId = (process.env.workerUID && process.env.workerUID.toString()) || "1";
        }
        return {
            type: 'dateFile',
            absolute: true,
            filename: path.join(this.logPath, logName + logId),
            maxLogSize: 1024 * 1024,
            backup: 3,
            pattern: "-yyyy-MM-dd.log",
            alwaysIncludePattern: true,
            category: logName
        }
    };

    getLogger(logName) {
        return log4js.getLogger(logName);
    }

    logConnect(logName, format) {
        let logger = this.getLogger(logName);

        //设置level为auto，遇到4xx和5xx会自动记录error级日志
        let logPattern = {
            level: 'auto',
            format: format || this.logFormat.join(' |')
        };
        return log4js.connectLogger(logger, logPattern);
    }
}

export { Logger }